package com.sandking.db.jedis;

import com.sandking.db.dao.YhljxxDao;
import com.sandking.db.bean.Yhljxx;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class YhljxxJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhljxx getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhljxx yhljxx = null;
		try{
			String key = "Yhljxx_Object";
			String field = SK_Plus.b("id:",id).e();
			yhljxx = Yhljxx.createForJson(jedis.hget(key,field));
			if(yhljxx == null){
				yhljxx = YhljxxDao.getById(id);
				if(yhljxx!=null){
					loadCache(yhljxx);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhljxx;
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static Yhljxx getByYh_id(int yh_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhljxx yhljxx = null;
		try{
			String key = "Yhljxx_Index";
			String field = SK_Plus.b("yh_id:",yh_id).e();
			String primaryKey = jedis.hget(key,field);
			if(primaryKey!=null){
				yhljxx = getById(Integer.valueOf(primaryKey));
			}
			if(yhljxx == null){
				yhljxx = YhljxxDao.getByYh_id(yh_id);
				if(yhljxx!=null){
					loadCache(yhljxx);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhljxx;
	}
	

	public static void loadCache(Yhljxx yhljxx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhljxx,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhljxx yhljxx,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhljxx_Object";
		String field = SK_Plus.b("id:",yhljxx.getId()).e();
		String data = yhljxx.toJson();
		p.hset(key,field,data);
		
		// ---------------------- 唯一索引 ----------------------
		String primaryKey = String.valueOf(yhljxx.getId());
		key = "Yhljxx_Index";
		
		field = SK_Plus.b("yh_id:",yhljxx.getYh_id()).e();
		p.hset(key,field,primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhljxx> yhljxxs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhljxx yhljxx : yhljxxs){
				loadCache(yhljxx,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhljxx yhljxx){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhljxx,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhljxx yhljxx,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhljxx_Object";
		String field = SK_Plus.b("id:",yhljxx.getId()).e();
		p.hdel(key,field);
		
		// ---------------------- 唯一索引 ----------------------
		key = "Yhljxx_Index";
		
		field = SK_Plus.b("yh_id:",yhljxx.getYh_id()).e();
		p.hdel(key,field);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhljxx> yhljxxs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhljxx yhljxx : yhljxxs){
				clearCache(yhljxx,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhljxx insert(Yhljxx yhljxx){
		yhljxx = YhljxxDao.insert(yhljxx);
		if(yhljxx!=null){
			loadCache(yhljxx);
		}
    	return yhljxx;
    }
    
    public static Yhljxx update(Yhljxx yhljxx){
    	yhljxx = YhljxxDao.update(yhljxx);
    	if(yhljxx!=null){
			loadCache(yhljxx);
		}
    	return yhljxx;
    }
    
    public static boolean delete(Yhljxx yhljxx){
    	boolean bool = YhljxxDao.delete(yhljxx);
    	if(bool){
    		clearCache(yhljxx);
    	}
    	return bool;
    }
}